package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Article;
import com.qilu.pe.dao.bean.ArticleDetail;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ArticleDetailActivity extends Base2Activity {
    private WebView webview;
    private TextView tv_1, tv_2, tv_3, tv_title1;

    private Article article;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_article_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview = findViewById(R.id.webview);
        tv_title1 = findViewById(R.id.tv_title1);
        tv_1 = findViewById(R.id.tv_1);
        tv_2 = findViewById(R.id.tv_2);
        tv_3 = findViewById(R.id.tv_3);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            article = (Article) bundle.getSerializable("article");
        }

        setHeadTitle("文章详情");

        hideProgress();
        reqArticleDetail();
        showProgress();
    }

    private Disposable reqArticleDetail() {
        return APIRetrofit.getDefault().reqArticleDetail(article.getId())
                .compose(RxUtil.<BaseResult2<ArticleDetail>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<ArticleDetail>>() {
                    @Override
                    public void accept(BaseResult2<ArticleDetail> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            initWebview(result2.getData().getText());
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }

                        ArticleDetail detail = result2.getData();
                        tv_title1.setText(detail.getName());
                        tv_1.setText("作者:" + detail.getAuthor());
                        tv_2.setText(detail.getCreated_at());
                        tv_3.setText(detail.getCount());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private void initWebview(String url) {
        // 开启js支持
        webview.getSettings().setJavaScriptEnabled(true);
        //2、关闭自动适应
        webview.getSettings().setUseWideViewPort(false);
        webview.getSettings().setLoadWithOverviewMode(false);
        // 3、//重写WebViewClient中的onPageFinished方法
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //修改图片大小
                int screenWidth = ScreenUtils.getScreenWidth();
//                String width = String.valueOf(screenWidth - SizeUtils.dp2px(50));
//                String width2 = String.valueOf(screenWidth - SizeUtils.dp2px(10));
                String width2 = String.valueOf(SizeUtils.px2dp(screenWidth) - 15);

                String javascript = "javascript:function ResizeImages() {" +
                        "var myimg,oldwidth;" +
                        "var maxwidth = document.body.clientWidth;" +
                        "for(i=0;i <document.images.length;i++){" +
                        "myimg = document.images[i];" +
//                        "myimg.margin = 0;" +
                        "if(myimg.width > " + width2 + "){" +
                        "oldwidth = myimg.width;" +
                        "myimg.width =" + width2 + ";" +
                        "}" +
                        "}" +
                        "}";
                view.loadUrl(javascript);
                view.loadUrl("javascript:ResizeImages();");
            }
        });
        webview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
//        webview.loadUrl(url);
        webview.loadData(url, "text/html;charset=utf-8", null); // 加载定义的代码，并设定编码格式和字符集。
    }
}